﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CheckersClientApplication.GameRoomServiceReference;
using CheckersClientApplication.Utility;

namespace CheckersClientApplication
{
    /// <summary>
    /// Interaction logic for GameRoomList.xaml
    /// </summary>
    public partial class GameRoomList : Window
    {
        private LoginServiceReference.clsPlayer objLPlayer;
        private GameRoomServiceReference.clsPlayer objGMPlayer;

        LoginServiceReference.LoginServiceClient client_login ;
        GameRoomServiceReference.GameRoomServiceClient client_gameroom;

        public GameRoomList()
        {
            MessageBox.Show("Game room list without Player Object is not exceptable It may be due to corruption of the Application Please consider reinstalling or re-running");
            throw new Exception("Game room list without Player Object is not exceptable It may be due to corruption of the Application Please consider reinstalling or re-running");
        }

        public GameRoomList(LoginServiceReference.clsPlayer objclsPlayer)
        {
            client_login = new LoginServiceReference.LoginServiceClient();
            client_gameroom = new GameRoomServiceReference.GameRoomServiceClient();

            this.objLPlayer = objclsPlayer;
            this.objGMPlayer = new GameRoomServiceReference.clsPlayer()
            {
                PlayerId = objLPlayer.PlayerId,
                PlayerName = objLPlayer.PlayerName,
                PlayerUserName = objLPlayer.PlayerUserName,
                PlayerPassword = objLPlayer.PlayerPassword
            };

            InitializeComponent();
            InitializeList();
            InitializeUI();
            


        }

        private void InitializeUI() 
        {
            
            try
            {
                lblPlayerName.Content = "Welcome " + objLPlayer.PlayerName;
            }
            
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private void InitializeList() 
        {

            try
            {
                var gameroomList = client_gameroom.GetGameRoomList();

                gameRoomList.Items.Clear();

                foreach (clsGameRoom room in gameroomList)
                {
                    Button enterBtn = new Button();
                    enterBtn.Content = "Enter";
                    enterBtn.Width = 50;
                    enterBtn.Height = 22;
                    enterBtn.BorderBrush = new SolidColorBrush(Colors.AntiqueWhite);
                    enterBtn.Tag = new PlayerGameRoomViewModel()
                                       {
                                           player = objGMPlayer,
                                           gameRoom = room
                                       };
                    enterBtn.Click += enterButtonHandler_click;



                    Label label1 = new Label();
                    label1.Width = 110;
                    label1.Content = "Game Id : " + room.GameId;

                    Label label2 = new Label();
                    label2.Width = 120;
                    label2.Content = "Status:  " + Enum.GetName(typeof (GameStatus), room.GameStatus);

                    Label label3 = new Label();
                    label3.Width = 210;
                    label3.Content = "Created At " + room.Timestamp;


                    StackPanel panel = new StackPanel();
                    panel.Orientation = Orientation.Horizontal;
                    panel = new StackPanel();
                    panel.Orientation = Orientation.Horizontal;


                    panel.Children.Add(label1);
                    panel.Children.Add(label2);
                    panel.Children.Add(label3);
                    panel.Children.Add(enterBtn);

                    gameRoomList.Items.Add(panel);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            
        }

        private void btnCreateGame_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var objClsGameRoom = client_gameroom.CreateGameRoom(objGMPlayer);

                PlayerGameRoomViewModel objGameRoomPlayerViewModel = new PlayerGameRoomViewModel() 
                {
                    player = objGMPlayer,
                    gameRoom = objClsGameRoom
                };

                PlayRegionScreen screen = new PlayRegionScreen(objGameRoomPlayerViewModel);
            
                screen.Show();
            
                this.Hide();
            }
            
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }


        

        private void enterButtonHandler_click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button playbutton = (Button)e.Source;
                PlayerGameRoomViewModel objGameRoomViewModel = (PlayerGameRoomViewModel)playbutton.Tag;

                var objclsGameRoom = client_gameroom.JoinGameRoom(objGameRoomViewModel.player, objGameRoomViewModel.gameRoom);

                PlayRegionScreen screen = new PlayRegionScreen(objGameRoomViewModel);
                screen.Show();
                this.Hide();

            }
            
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private void btnReferesh_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                InitializeList();
            }

            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
    }


    public class PlayerGameRoomViewModel 
    {
        public clsGameRoom gameRoom;
        public clsPlayer player;

    }
}
